using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;

public class Quiz : MonoBehaviour
{
   [SerializeField]TextMeshProUGUI questionText;
   [SerializeField]QuestionSO question;
   [SerializeField]GameObject[] answerButtons;
   
   int currentAnswerIndex;
   [SerializeField] Sprite defaultAnswerSprite; 
   [SerializeField] Sprite correctAnswerSprite; 
    void Start()
    {
        questionText.text = question.GetQuestion();
        for (int i = 0; i < answerButtons.Length; i++)
        {
            TextMeshProUGUI buttonText = answerButtons[i].GetComponentInChildren<TextMeshProUGUI>();
            buttonText.text = question.GetAnswer(i);
        }
    }

    public void OnAnswerSelected(int index)
    {
        Image buttonImage;
        if (index == question.GetCorrectAnswer())
        {
            questionText.text = "Correct!";
            buttonImage = answerButtons[index].GetComponent<Image>();
            buttonImage.sprite = correctAnswerSprite;
        }
        else
        {
            currentAnswerIndex = question.GetCorrectAnswer();
            string correctAnswer = question.GetAnswer(currentAnswerIndex);
            questionText.text = "The correct answer was \n" + correctAnswer;
            buttonImage = answerButtons[currentAnswerIndex].GetComponent<Image>();
            buttonImage.sprite = correctAnswerSprite;
        }
    }
    
}
